﻿using System.Collections.Generic;
using System.Linq;
using GTAuto.DAL.Entities;
using GTAuto.DAL.Enums;
using GTAuto.DAL.Extensions;

namespace GTAuto.DAL.DAL
{
    public class DALModels : DALBase
    {
        public static DALModels Singleton = new DALModels();

        public GTModel Add(GTModel gtModel)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var newModel = new Model();
                newModel.Name = gtModel.Name;
                newModel.BrandId = gtModel.BrandId;
                newModel.Active = gtModel.Active;

                newModel.Logo = gtModel.Logo;
                newModel.LogoSmal = gtModel.LogoSmal;
                newModel.Description = gtModel.Description;


                db.Models.InsertOnSubmit(newModel);
                db.SubmitChanges();
                gtModel.Id = newModel.Id;
                return gtModel;
            }
        }

        public GTModel Update(GTModel gtBrandModel)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var gtModel = db.Models.SingleOrDefault(x => x.Id == gtBrandModel.Id);
                gtModel.Name = gtBrandModel.Name;
                gtModel.BrandId = gtBrandModel.BrandId;
                gtModel.Active = gtBrandModel.Active;
                gtModel.Year = gtBrandModel.Year;

                gtModel.Logo = gtBrandModel.Logo;
                gtModel.LogoSmal = gtBrandModel.LogoSmal;
                gtModel.Description = gtBrandModel.Description;

                db.SubmitChanges();
                return gtBrandModel;
            }
        }


        public GTModel Get(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var model = db.Models.SingleOrDefault(x => x.Id == id);
                GTModel gtModel = new GTModel();
                gtModel.UpdateTo(model);
                return gtModel;
            }
        }

        public List<GTModel> GetPaged(int page, int count, out int itemsCount)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var brds =
                 from p in db.Models
                 orderby p.Name
                 select p;


                var models = brds.ToList();

                itemsCount = models.Count;

                var ret = new List<GTModel>();
                foreach (var model in models.Page(page, count))
                {
                    GTModel gtBrand = new GTModel();
                    gtBrand.UpdateTo(model);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }

        public List<GTModel> GetPaged(BrandTypeEnum brandTypeEnum, int page, int count, out int itemsCount)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var brds =
                 from p in db.Models
                 where p.Brand.Type == (decimal)brandTypeEnum
                 orderby p.Name
                 select p;


                var models = brds.ToList();

                itemsCount = models.Count;

                var ret = new List<GTModel>();
                foreach (var model in models.Page(page, count))
                {
                    GTModel gtBrand = new GTModel();
                    gtBrand.UpdateTo(model);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }





        public void Delete(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                Model singleOrDefault = db.Models.SingleOrDefault(x => x.Id == id);

                if (singleOrDefault != null)
                {
                    db.Models.DeleteOnSubmit(singleOrDefault);
                    db.SubmitChanges();
                }
            }
        }


        public List<GTModel> GetByBrand(int brandId, int page, int count, out int itemsCount)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var brds =
                 from p in db.Models
                 where p.BrandId == brandId
                 orderby p.Name
                 select p;

                var brands = brds.ToList();

                itemsCount = brands.Count;

                var ret = new List<GTModel>();

                foreach (var brand in brands.Page(page, count))
                {
                    GTModel gtBrand = new GTModel();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }

        public List<GTModel> GetByPropertyValue(int brandId, int propertyId,string propertyValue)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var temp =  db.ModelWithPropertyValue(brandId, propertyId, propertyValue).ToList();


                return temp.Select(modelWithPropertyValueResult => Get((int) modelWithPropertyValueResult.modelId)).ToList();
            }
        }



    }
}
